<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\CarModel;
use App\Models\CarBrand;
use App\Models\CarSerie;
class CarModelController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $param = [];
        $query  = CarModel::query();
        if($request->keyword) {
            $query->where('name',
                'like','%'.$request->keyword.'%');
            $param['keyword'] = $request->keyword;
        }
        $list   = $query->orderBy('created_at','desc')->paginate(10);
        $brands = CarBrand::get();
        return view('admin.car-model.index',compact('list','brands','param'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except(['_token']);
        CarModel::create($data);
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = CarModel::find($id);
        $brands = CarBrand::get();
        $series = CarSerie::get();
        return view('admin.car-model.edit',compact('data','brands','series'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except(['_token','_method']);
        $flag = CarModel::where(['id'=>$id])->update($data);
        return redirect('zadmin/car-models');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs['status'] = 'success';
        $rs['msg'] = '操作成功';
        CarModel::destroy($id);
        return back()->with('rs',$rs);

    }
}
